﻿/*********************************************************************
 版权所有: 版权所有(C) 2009 ,熙软研发部
 模块编号:  DishAdd.aspx.cs
 作    者:范徐超
 编写日期:2009-09-13
 审    核:
 审核日期:
 描    述:菜式添加页面
...........................................................................
如果模块有修改，则每次修改添加以下注释(以系统上线后为基准)：

 *********************************************************************/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cisoft.Booking.Model;
using Cisoft.Booking.BLL;

public partial class Business_Booking_DishAdd : WebUI.Page.BasePage
{
    private DishBO dishBO = new DishBO();
    private DishMO dishMO = new DishMO();

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    #region 按钮事件
    /// <summary>
    /// 新增事件
    /// </summary>
    protected void btnSave_Click(object sender, ImageClickEventArgs e)
    {
        int i = 0;
        string strError = "";
        dishMO = GetModel();
        //判断是否有重名菜式
        if (IsReapte(tb_DishName.Text.Trim()))
        {
            PageError("该菜式名称已经存在！", Request.RawUrl);
        }
        if (dishBO.ExistsByCode(tb_DishCode.Text))
        {
            PageError("该菜式代码已经存在！", Request.RawUrl);
        }
        try
        {
            i = dishBO.Add(dishMO);
        }
        catch (Exception ex)
        {
            strError = ex.Message;
        }
        if (i > 0)
        {
            PageRight("操作成功！", "DishManagement.aspx", "返回管理", "");
        }
        else
        {
            PageError(strError, Request.RawUrl);
        }
    }
    /// <summary>
    /// 返回事件
    /// </summary>
    protected void butrReset_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("DishManagement.aspx");
    }
    #endregion

    #region 填充规则实体类 private RuleMO GetModel()
    /// <summary>
    /// 填充规则实体类 
    /// </summary>
    private DishMO GetModel()
    {
        string dishname = tb_DishName.Text.Trim();
        string dishcode = tb_DishCode.Text.Trim();
        string dishprice = tb_DishPrice.Text.Trim();
        string dishpic = GetPath();
        string dishremark = tb_DishRemark.Text;

        dishMO.DishID = dishBO.GetMaxId();
        dishMO.DishName = dishname;
        dishMO.DishCode = dishcode;
        dishMO.DishPic = dishpic;
        dishMO.DishPrice = Convert.ToDecimal(dishprice);
        dishMO.DishRemark = dishremark;

        return dishMO;
    }
    #endregion

    #region 获得上传控图片路径
    /// <summary>
    /// 获得上传控图片路径
    /// </summary>
    private string GetPath()
    {
        string path = "";
        //if (ViewState["ImagePath"] != null)
        //{
        //    path = ViewState["ImagePath"].ToString();
        //}
        
        return path;
    }
    #endregion

    #region 图片上传事件 
    protected void lbtn_UploadImage_Click(object sender, EventArgs e)
    {
        //if (!string.IsNullOrEmpty(InputFile1.FileName))
        //{
        //    if (InputFile1.HasFile)
        //    {
        //        //判断是否为JPG或GIF格式
        //        if (!IsExtension(InputFile1.FileName))
        //        {
        //            PageError("上传的文件必须为jpg或gif格式！", Request.RawUrl);
        //        }
        //        //文件名
        //        string filename = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(InputFile1.FileName);
        //        //保存文件路径
        //        string filefullname = System.IO.Path.Combine(Request.PhysicalApplicationPath + "\\Upload", filename);
        //        //上传文件
        //        InputFile1.MoveTo(filefullname, Brettle.Web.NeatUpload.MoveToOptions.Overwrite);
        //        //显示讯息
        //        double size = Math.Round(InputFile1.ContentLength / 1024F, 2);
        //        //Label1.Text = string.Format("来源:{0} ({1}kb)<br/>上传文件名称：{2}。", InputFile1.FileName, size, filename);

        //        //image1.Src = Server.MapPath(filefullname.Replace(@"\\",@"\"));
        //        Image1.ImageUrl = "../../../Upload/" + filename;

        //        ViewState["ImagePath"] = Image1.ImageUrl;
        //    }
        //}
    }
    #endregion

    #region 判断是否有重名菜式 bool IsReapte(string BoxName)
    /// <summary>
    /// 判断是否有重名菜式,有重名返回true,无重名返回false
    /// </summary>
    /// <param name="BoxName">新增的包厢名称</param>
    private bool IsReapte(string DishName)
    {
        bool flag = false;
        DataSet ds = dishBO.GetAllList();
        if (ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (DishName == dr["DishName"].ToString())
                {
                    flag = true;
                    break;
                }
            }
        }
        return flag;
    }
    #endregion

    #region 判断图片是否为指定格式
    private bool IsExtension(string strInput)
    {
        string last = System.IO.Path.GetExtension(strInput);
        //if (last != ".jpg" || last != ".jpeg" || last != ".JPG" || last != ".JPEG" || last != ".gif" || last != ".GIF")
        //{
        //    return false;
        //}
        //else
        //{
        //    return true;
        //}
        if (".jpg.jpeg.gif.JPG.JPEG.GIF".IndexOf(last) >= 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    #endregion
}
